<?php

namespace App\Admin\Controllers;

use App\Models\Article;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;

class ArticleController extends Controller
{
    use HasResourceActions;

    //文章首页
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header('文章列表');
            $content->body($this->grid());
        });
    }

    //编辑文章
    public function edit($id, Content $content)
    {
        return $content->header('文章')->description('编辑')->body($this->form()->edit($id));
    }

    //编辑文章
    public function update($id)
    {
        return $this->form()->update($id);
    }

    //查看文章详情
    public function show($id, Content $content)
    {
        return $content
            ->header('文章信息')
            ->description('详情')
            ->body($this->detail($id));
    }

    //添加文章
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建文章');
            $content->body($this->form());
        });
    }


    //添加文章保存回调
    public function store()
    {
        return $this->form()->store();
    }

    //获取文章
    protected function grid()
    {
        return Admin::grid(Article::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->name('文章名称')->style('max-width:200px;word-break:break-all;');
            $grid->type('文章类别')->display(function ($value) {
                 switch ($value) {
                     case '1':
                         return 'banner';
                     case '2':
                         return '咨询';
                 }
            });
            $grid->is_show('是否显示')->display(function ($value) {
                return ($value==1) ? '是' : '否';
            });

            $grid->model()->orderBy('id', 'desc');

            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
                $filter->column(1/3, function ($filter) {
                    $filter->like('name', '文章名称');
                });
            });

            //禁用导出按钮
            $grid->disableExport();
        });
    }

    //修改表单事项
    protected function form()
    {
        // 创建一个表单
        return Admin::form(Article::class, function (Form $form) {
            $form->text('name', '文章名称')->rules('required');
            $form->image('picture', '展示图片')->rules('image');
            $form->select('type', '文章类别')->options([1 => 'banner', 2 => '资讯'])->default('1')->rules('required');
            $form->editor('content', '文章内容');
            $form->radio('is_show', '是否显示')->options([1 => '是', 0 => '否'])->default(0);

            // 定义事件回调，当模型即将保存时会触发这个回调
            $form->saving(function (Form $form) {
            });
            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
            });

            $form->footer(function ($footer) {

                // 去掉`重置`按钮
                $footer->disableReset();

            });
        });
    }

    /**
     * 文章详情
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Article::findOrFail($id));
        $show->id('文章ID');
        $show->name('文章名称');
        $show->type('文章类别')->using([1 => 'banner', 2 => '资讯']);
        $show->picture('展示图片')->image();
        $show->content('文章内容')->unescape();
        $show->is_show('是否显示')->using([0 => '否', 1 => '是']);
        

        $show->panel()->tools(function ($tools) {
            $tools->disableDelete();
        });

        return $show;
    }
}
